<style type="text/css">
    .layui-table-body{overflow-x:auto;}
    .layui-table-cell{font-size:12px;}
</style>
<div class="layui-form hisi-search">
    <form action="{:url()}" method="get" id="hisi-table-search">
        <div class="layui-form-item">
            <label class="layui-form-label">会员</label>
            <div class="layui-input-inline">
                <select name="user_id" class="layui-input field-user_id" lay-verify="required" lay-search="">
                  <option value="">直接选择或搜索选择</option>
                  {volist name="users" id='vo'}
                  <option value="{$vo.id}">{$vo.nick}</option>
                  {/volist}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">当前进度</label>
            <div class="layui-input-inline">
                <select name="status" class="layui-input field-status" lay-verify="required" lay-search="">
                  <option value="">直接选择或搜索选择</option>
                  <option value="0">顾问开单</option>
                  <option value="1">库管配料</option>
                  <option value="2">前台结算</option>
                  <option value="3">财务审核</option>
                  <option value="4">完成</option>
                  <option value="5">预约</option>
                  <option value="6">数据异常，待补录</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">操作单号</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input field-sn" id="test6" name="sn" placeholder="请输入操作单号">
            </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">开始时间</label>
          <div class="layui-input-inline">
            <!-- <input type="text" class="layui-input field-start_end" id="test6" name="start_end" placeholder=" - "> -->
            <input type="text" class="layui-input field-start" id="start" name="start" placeholder="yyyy-MM-dd">
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">结束时间</label>
          <div class="layui-input-inline">
            <input type="text" class="layui-input field-end" id="end" name="end" placeholder="yyyy-MM-dd">
          </div>
        </div>
        <input type="hidden" name="state" value="0">
        <div class="layui-form-item hisi-search-btn">

            <div class="layui-input-inline">
                <button type="submit" class="layui-btn layui-btn-normal layui-icon layui-icon-search">执行</button>
                <button type="button" class="layui-btn layui-btn-warm layui-icon layui-icon-download-circle export">导出</button>
            </div>
        </div>
    </form>
</div>

<table id="dataTable"  lay-filter="dataTable"></table>

<script type="text/html" id="statusTpl">
    {{# if (d.status == 0){ }}
    <span style="color:Red">顾问开单  {{# if(d.is_repair == 1){ }} (补单)  {{# } }} </span>
    {{# }else if (d.status == 1){ }}
    <span style="color:Orange">库管配料   {{# if(d.is_repair == 1){ }} (补单)  {{# } }}</span>
    {{# }else if (d.status == 2){ }}
    <span style="color:Yellow">前台结算   {{# if(d.is_repair == 1){ }} (补单)  {{# } }}</span>
    {{# }else if (d.status == 3){ }}
    <span style="color:Green">财务审核   {{# if(d.is_repair == 1){ }} (补单)  {{# } }}</span>
    {{# }else if (d.status == 4){ }}
    <span style="color:Blue">完成</span>
    {{# }else if (d.status == 6){ }}
    <span style="color:Indigo">数据异常，待补录</span>
    {{# }else if (d.status == 5){ }}
    <span style="color:Violet">预约
         {{# if(d.is_repair == 1){ }} (补单)  {{# } }}</span>
    {{# } }}
</script>
<script type="text/html" title="操作按钮模板" id="buttonTpl">
    {{# if (d.status == 0){ }}
    <a href="{:url('gwkd')}?id={{ d.id }}" class="layui-btn layui-btn-xs layui-btn-warm hisi-iframe-pop"  hisi-data="{width: '1100px', height: '700px', title: '开单'}">开单</a>
    {{# }else if (d.status == 1){ }}
    <a href="{:url('kgkd')}?id={{ d.id }}" class="layui-btn layui-btn-xs layui-btn-warm hisi-iframe-pop"  hisi-data="{width: '1100px', height: '700px', title: '开单'}">开单</a>
    {{# }else if (d.status == 2){ }}
    <a href="{:url('sykd')}?id={{ d.id }}" class="layui-btn layui-btn-xs layui-btn-warm hisi-iframe-pop"  hisi-data="{width: '1100px', height: '700px', title: '开单'}">开单</a>

    {{# }else if (d.status == 3){ }}
    <a href="{:url('cwsh')}?id={{ d.id }}" class="layui-btn layui-btn-xs layui-btn-warm hisi-iframe-pop"  hisi-data="{width: '1100px', height: '700px', title: '开单'}">财务审核</a>
    <a href="{:url('cwsh')}?id={{ d.id }}&xx=2" target="_blank" class="layui-btn layui-btn-xs layui-btn-primary ">打印单据</a>
    {{# }else if (d.status == 4){ }}
    <a href="javascript:;" class="layui-btn layui-btn-xs layui-btn-warm " >完成</a>
    {{# }else if (d.status == 6){ }}
    <a href="{:url('cwsh')}?id={{ d.id }}" class="layui-btn layui-btn-xs layui-btn-warm hisi-iframe-pop"  hisi-data="{width: '1100px', height: '700px', title: '开单'}">财务审核</a>
    {{# }else if (d.status == 5){ }}
    <span style="color:Violet">预约</span>
    {{# } }}
    <a href="{:url('lookOperation')}?id={{ d.id }}" class="layui-btn layui-btn-xs layui-btn-normal hisi-iframe-pop"  hisi-data="{width: '1100px', height: '700px', title: '详情'}">查看详情</a>

    {{# if (d.status == 6){ }}
    <!-- <a href="{:url('edit')}?id={{ d.id }}" class="layui-btn layui-btn-xs layui-btn-normal hisi-iframe-pop"  hisi-data="{width: '550px', height: '420px', title: '修改'}">修改</a> -->
    <a href="{:url('removeSub')}?id={{ d.id }}" class="layui-btn layui-btn-xs layui-btn-danger j-tr-del">删除</a>
    {{# } }}
    {{# if (d.status < 3){ }}
    <a href="{:url('del')}?id={{ d.id }}" class="layui-btn layui-btn-xs layui-btn-danger j-tr-del">删除</a>
    {{# } }}
</script>
{include file="system@block/layui" /}
<script id="toolbar" type="text/html">
    <!-- <a href="{:url('add')}" class="layui-btn layui-btn-normal layui-btn-sm hisi-iframe-pop" hisi-data="{width: '550px', height: '420px', title: '添加'}">&nbsp;添加</a> -->
</script>
<script type="text/javascript">
  layui.use(['table','laydate','jquery','excel'], function() {
      var table = layui.table;
      var laydate = layui.laydate;
      var $ = layui.jquery;
      var excel = layui.excel;
      $(document).on('click','.export',function(){
        var form = $('#hisi-table-search').serializeArray();
        var where = new Array();
        form.push({name:'type',value:2});
        $.ajax({
           url: '{:url()}',
           data:form,
           dataType: 'json',
           success: function(res) {
               // 假如返回的 res.data 是需要导出的列表数据
               console.log(res.data);// [{name: 'wang', age: 18, sex: '男'}, {name: 'layui', age: 3, sex: '女'}]
               // 1. 数组头部新增表头
               res.data.unshift({
                 sn:'订单号',
                 status_text:'状态',
                 shop_name:'门店',
                 user_nick: '会员',
                 room_name:'房间',
                 adviser:'预约顾问',
                 technician:'美容师',
                 projects:'预约项目',
                 cashier:'收银',
                 warehouse_admin:'库管',
                 finance:'财务',
                 createtime:'开单时间',
                 });
               // 2. 如果需要调整顺序，请执行梳理函数
               var data = excel.filterExportData(res.data, [
                'sn',
                'status_text',
                'shop_name',
                'user_nick',
                'room_name',
                'adviser',
                'technician',
                'projects',
                'cashier',
                'warehouse_admin',
                'finance',
                'createtime',
               ]);
               // 3. 执行导出函数，系统会弹出弹框
               excel.exportExcel({
                   sheet1: data
               }, '操作单.xlsx', 'xlsx');
           }
       });
      })
        //日期范围
        laydate.render({
        elem: '#start'
        });
        laydate.render({
        elem: '#end'
        });


        table.render({
            elem: '#dataTable'
            ,url: '{:url('index')}?state=0' //数据接口
            ,page: true //开启分页
            ,skin: 'row'
            // ,width:1000
            ,even: true
            ,limit: 20
            ,text: {
                none : '暂无相关数据'
            }
            ,toolbar: '#toolbar'
            ,defaultToolbar: ['filter']
            ,cols: [[ //表头
                {type:'checkbox'}
                ,{title: '操作', width: 210, templet: '#buttonTpl'}
                ,{field: 'status', width:150, title: '当前进度', templet: '#statusTpl'}
                ,{field: 'sn', width:180, title: '操作单号'}
                ,{field: 'shop_id', width:120, title: '门店名称',templet:function(d){
                    return d.has_shop ? d.has_shop.name :''
                }}
                ,{field: 'user_id', width:100, title: '会员',templet:function(d){
                    return d.has_user ? d.has_user.nick :''
                }}
                ,{field: 'room_id', width:100, title: '房间',templet:function(d){
                    return d.has_room ? d.has_room.name : ''
                }}
                ,{field: 'adviser', width:80, title: '预约顾问'}
                ,{field: 'technician_ids', width:120, title: '预约美容师',templet:function(d){
                    return d.technician;
                }}
                ,{field: 'projects', width:120, title: '预约项目'}
                ,{field: 'cashier', width:120, title: '收银'}
                ,{field: 'warehouse_admin', width:120, title: '库管'}
                ,{field: 'finance', width:120, title: '财务'}
                ,{field: 'createtime', width:120, title: '开单时间', edit: 'text',event:'date',data_field: "dBeginDate" ,sort:true}
                ,{field: 'updatetime', width:120, title: '更新时间'}
            ]]
            ,data:[{
              dBeginDate: '',
            }]
            ,done: function(){
                if (typeof(layui.global.addTableTool) !== 'undefined') {
                    layui.global.addTableTool();
                }
            }
        });

        //监听单元格事件
    table.on('tool(dataTable)', function (obj) {
        var data = obj.data;
        var newdata = {};
        newdata['id'] = data.id;

        if (obj.event === 'date') {
            var field = $(this).data('field');

            laydate.render({
                elem: this.firstChild
                , show: true //直接显示
                , closeStop: this
                , type: 'datetime'
                , format: "yyyy-MM-dd HH:mm:ss"
                , done: function (value, date) {
                      newdata[field] = value;
                      console.log(newdata);
                      obj.update(newdata);
                      $.ajax({
                          url:"{:url('editKaidanTime')}",
                          type:"post",
                          data:newdata,
                          success:function(res){
                              layer.msg(res.message)
                          }

                      })
                      // console.log(JSON.stringify(table.cache.dataTable))
                }
            });
        }
      })
    });
</script>
